package cn.ibobo.lease.admin.controller;

import cn.ibobo.lease.admin.service.impl.UserServiceImpl;
import cn.ibobo.lease.common.dto.UserCouponDTO;
import cn.ibobo.lease.common.dto.UserDTO;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.ListCopyToVOUtil;
import cn.ibobo.lease.common.util.PaginationVOUtil;
import cn.ibobo.lease.common.vo.ResultVO;
import cn.ibobo.lease.common.util.ResultVOUtil;
import cn.ibobo.lease.common.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description 会员信息表控制器
 * @Author lililiang
 * @Date 2018-12-13 18:36:40
 */

@RestController
@Api(tags = "会员管理")
@RequestMapping("user")
public class UserController{

    private static final Log LOGGER = LogFactory.getLog(UserController.class);

    @Autowired
    private UserServiceImpl service;
    @Autowired
    private ResultVOUtil<Object> resultObject;

    /**
	 * 会员信息表列表
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
	 * @Param
	 * @Return
	 */
    @ApiOperation(value = "会员信息表列表", response = UserVO.class)
    @PostMapping("list4Page")
    public ResultVO<Object> list4Page(
            @ApiParam(value = "昵称",required = false) @RequestParam(required = false) String nickname,
            @ApiParam(value = "手机号",required = false) @RequestParam(required = false) String mobile,
            @ApiParam(value = "账号",required = false) @RequestParam(required = false) String name,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ){
        try {
            UserDTO entityDTO = new UserDTO();
            entityDTO.setNickname(nickname);
            entityDTO.setMobile(mobile);
            entityDTO.setName(name);
            List<UserDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), PaginationVOUtil.getPaginationVO(list, UserVO.class, pageNum));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 会员信息表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @ApiOperation(value = "会员信息表列表（无分页）", response = UserVO.class)
    @PostMapping("listByCondition")
    public ResultVO<Object> listByCondition(
            @ApiParam(value = "昵称",required = false) @RequestParam(required = false) String nickname,
            @ApiParam(value = "手机号",required = false) @RequestParam(required = false) String mobile,
            @ApiParam(value = "账号",required = false) @RequestParam(required = false) String name
    ){
        try {
            UserDTO entityDTO = new UserDTO();
            entityDTO.setNickname(nickname);
            entityDTO.setMobile(mobile);
            entityDTO.setName(name);
            List<UserDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, UserVO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 会员信息表详细信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
//    @ApiOperation(value = "会员信息表详细信息", response = UserVO.class)
//    @PostMapping("queryOneByPk")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") String id){
        try {
            UserDTO entityDTO = service.queryOneByPk(id);
            UserVO entity = new UserVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存会员信息表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
//    @ApiOperation(value = "保存会员信息表信息", response = ResultVO.class)
//    @PostMapping("save")
    public ResultVO<Object> save(UserVO entity) {
        try {
            UserDTO entityDTO = new UserDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            if(service.save(entityDTO) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * @Description 会员充/退押金
     * @Author lililiang
     * @Date 2019/2/13 19:01
     * @Param [uid, deposit, type]
     * @Return cn.ibobo.lease.common.vo.ResultVO<java.lang.Object>
     */
    @ApiOperation(value = "会员充/退押金", response = ResultVO.class)
    @PostMapping("deposit")
    public ResultVO<Object> deposit(
            @ApiParam(value = "用户id",required = true) @RequestParam(required = true) String uid,
            @ApiParam(value = "押金金额",required = true) @RequestParam(required = true) BigDecimal deposit,
            @ApiParam(value = "操作类型：1：充押金，2：退押金",required = true) @RequestParam(required = true) Byte type
    ) {
        try {
            UserDTO entityDTO = new UserDTO();
            entityDTO.setUid(uid);
            entityDTO.setDeposit(deposit);
            entityDTO.setType(type);
            if(service.deposit(entityDTO) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }


    /**
     * @Description 平台赠送点金券
     * @Author lililiang
     * @Date 2019/2/13 19:01
     * @Param [uid, deposit, type]
     * @Return cn.ibobo.lease.common.vo.ResultVO<java.lang.Object>
     */
    @ApiOperation(value = "平台赠送点金券", response = ResultVO.class)
    @PostMapping("coupon")
    public ResultVO<Object> coupon(
            @ApiParam(value = "用户id数组，多个id用，隔开",required = true) @RequestParam(required = true) String uid,
            @ApiParam(value = "点金金额",required = true) @RequestParam(required = true) BigDecimal coupon
    ) {
        try {
            UserCouponDTO entityDTO = new UserCouponDTO();
            entityDTO.setUid(uid);
            entityDTO.setMoney(coupon);
            Integer result = service.coupon(entityDTO);
            return resultObject.success(result, ResultEnum.getMsg(result));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }
    /**
     * 删除会员信息表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
//    @ApiOperation(value = "删除会员信息表信息", response = ResultVO.class)
//    @PostMapping("delete")
    public ResultVO<Object> delete(@ApiParam(value = "id",required = true) @RequestParam(value = "id") String id){
        try {
            if(service.delete(id) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

}